package com.yikome.uaa.service;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yikome.common.CommonService;
import com.yikome.uaa.domain.Authority;
import com.yikome.uaa.domain.Role;
import com.yikome.uaa.repository.RoleRepository;
import com.yikome.uaa.utils.UserAuthoritiesUtils;
import com.google.common.base.Preconditions;

@Service
public class RoleService extends CommonService<Role, Long>{

	@Autowired
	private RoleRepository roleRepository ; 
	
	@Autowired
	private NamedParameterJdbcTemplate jdbcTemplate ;
	
	@Autowired
	public void setResourceDao(RoleRepository repository) {
		super.setCommonDao(repository);
	}

	public List<Role> findRole(Role roleRequest) {
		
		String contentSQL = "SELECT * FROM u_role \n" ;
		
		StringBuilder paramSQL = new StringBuilder("WHERE u_role.yn = 1 \n") ; 
		Map<String , Object> paramMap = new HashedMap<>();
		
		if(StringUtils.isNotBlank(roleRequest.getName())) {
			paramSQL.append("AND u_role.name LIKE :name \n");
			paramMap.put("name", "%" + roleRequest.getName() + "%");
		}
		if(StringUtils.isNotBlank(roleRequest.getValue())) {
			paramSQL.append("AND u_role.value LIKE :value \n");
			paramMap.put("value", "%" + roleRequest.getValue() + "%");
		}
		return jdbcTemplate.query(contentSQL + paramSQL , paramMap, new BeanPropertyRowMapper<Role>(Role.class));
	}

	public Role findRoleDetails(Long roleId) {
		
		Role role = roleRepository.findById(roleId).orElse(null);
		
//		if(role != null && role.getAuthorities() != null && !role.getAuthorities().isEmpty()) {
//			Set<Authority> collectionUserAuthority = UserAuthoritiesUtils.collectionUserAuthority(role.getAuthorities());
//			role.setAuthorities(collectionUserAuthority);
//		}
		
		return role;
	}
	
	@Transactional
	public Boolean saveUpdateRole(Role roleRequest) {
		
		if(roleRequest.getId() != null) {
			Role oldRole = roleRepository.findById(roleRequest.getId()).orElse(null);
			Preconditions.checkArgument(oldRole != null, "角色不存在...");
			oldRole.getAuthorities().removeAll(oldRole.getAuthorities());
			roleRepository.save(oldRole);
			oldRole.setValue(roleRequest.getValue());
			oldRole.setName(roleRequest.getName());
			oldRole.setAuthorities(roleRequest.getAuthorities());
			roleRepository.save(oldRole);
		}else {
			Role byName = roleRepository.findByName(roleRequest.getName().trim());
			Preconditions.checkArgument(byName == null, "角色名已存在...");
			roleRepository.save(roleRequest);
		}
		return true;
	}
	
	@Transactional
	public Boolean deleteRole(Role oldRole) {
		roleRepository.delete(oldRole);
		return true;
	}
	
}
